<?php
/**
 * Class Tests represents the database table 'kontrolltoo'.
 * It has functions to manipulate database objects.
 */
class Test {
	
public function __construct( &$alt_sql = false){
		global $sql;
		if ($alt_sql==false){
			$this->sql=$sql;
		} else {
			$this->sql=&$alt_sql;
		}	
	}

	/**
	 * Adds a new test (kontrolltoo) to the database.
	 *
	 * @return boolean
	 */
	public static function insert($sql, $katseid, $kirjeldus, $avatud, $nimi ) {
		//global $sql;
		$query = "INSERT INTO kontrolltoo (kt_nimi, lubatud_katseid,
		 kirjeldus, avatud) 
			VALUES ('".mysql_escape_string($nimi)."',
			 '".$katseid."',
			 '".mysql_escape_string($kirjeldus)."',
			  '".$avatud."')";
		
		$sql->query($query);
		if( $sql->affectedRows() == 1 ) {
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * Edits test (kontrolltoo) to the database.
	 *
	 * @return boolean
	 */
	/*
	public function edit($sql, $id, $katseid, $kirjeldus, $avatud, $nimi ) {
		//global $sql;
		$query = "UPDATE kontrolltoo k SET k.katseid_lubatud ='".$katseid."',
		 k.nimi='".mysql_escape_string($nimi)."', k.kirjeldus = '".mysql_escape_string($kirjeldus)."',
		  k.avatud ='".$avatud."' WHERE k.idkontrolltoo = '".$id."'";
		
		$sql->query($query);
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM kontrolltoo k 
			WHEREk.katseid_lubatud ='".$katseid."' AND
			 k.nimi='".$nimi."' AND k.kirjeldus = '".$kirjeldus."' AND
		  k.avatud ='".$avatud."' AND k.idkontrolltoo = '".$id."'";
			$result = $sql->selectQquery($query);
			
			if(count($result)==1){
				return true;
				} else return false;
		} else return true;
	}
	
	*/
	/**
	 * Deletes a test.
	 *
	 * @param integer $testId
	 * @return boolean
	 */
	public static function delete($sql, $testId) {
		//global $sql;
		$query = "DELETE FROM kontrolltoo
			WHERE idkontrolltoo = ".$testId." 
			LIMIT 1";
		$result = $sql->query($query);
		
		if( $result ) {
			return true;
		}
		return false;
	}
	
	/**
	 * Gets open tests from the database that the user has not already completed.
	 * @return array
	 */
	public static function load($sql) {
		//global $sql;
		//$userId = $_SESSION['userId'];
		
		$query = "SELECT * 
			FROM kontrolltoo k 
			WHERE k.avatud = 1 
			ORDER BY k.idkontrolltoo DESC";
		
		$result = $sql->selectQuery( $query );
		if(!empty($result)){
			return $result;
		} else return false;
		
	}
	
	public static function loadName($sql, $id) {
		//global $sql;
		$query = "SELECT * 
			FROM kontrolltoo k 
			WHERE k.idkontrolltoo = ".$id." 
			ORDER BY k.idkontrolltoo DESC LIMIT 1" ;
		
		$result = $sql->selectQuery( $query );
		if(!empty($result)){
			return $result[0]['kt_nimi'];
		} else return false;
		
	}
	
	
	/**
	 * Gets all tests from the database.
	 * @return array
	 */
	public static function loadAll($sql) {
		//global $sql;
		$query = "SELECT * 
			FROM kontrolltoo
			ORDER BY idkontrolltoo DESC";
		
		$result = $sql->selectQuery( $query );
		if(!empty($result)){
			return $result;
		} else return false;
	
	}
	
	/**
	 * 
	 * returns true/false based on whether the test is open or not
	 * @param $sql
	 * @param int $id- id of the test
	 */
	
	public static function checkStatus($sql, $id) {
		//global $sql;
		
		
		$query = "SELECT k.avatud  
			FROM kontrolltoo k
			WHERE k.idkontrolltoo =".$id."
			LIMIT 1";
		
		$result = $sql->selectQuery( $query );
		
		if(empty($result)){
			return false;
		} else {
			if($result[0]['avatud'] == 1){
				return true;
			} else return false;
		}
	
	}
	
	/**
	 * changes the state open/closed of a test
	 * @param $sql- global sql object
	 * @param int $id- id of the test 
	 */
	public static function changeState($sql, $id) {
		//global $sql;
		
		
		$query = "SELECT k.avatud  
			FROM kontrolltoo k
			WHERE k.idkontrolltoo ='".$id."'
			LIMIT 1";
		
		$result = $sql->selectQuery( $query );
		
		if(empty($result)){
			return false;
		} else {
			if($result[0]['avatud'] == 1){
				$query = "UPDATE kontrolltoo k SET k.avatud = '0' WHERE k.idkontrolltoo = '".$id."'";
				
				$sql->query( $query );
				if( $sql->affectedRows() == 1 ) {
					return true;
				} else return false;
			} else {
				$query = "UPDATE kontrolltoo k SET k.avatud = '1' WHERE k.idkontrolltoo = '".$id."'";
				
				$sql->query( $query );
				if( $sql->affectedRows() == 1 ) {
					return true;
				} else return false;
			}
	
		}
	
	
	}
	
	/**
	 * changes the state open/closed of a test
	 * @param $sql- global sql object
	 * @param int $id- id of the test 
	 */
	public static function alterTest($sql, $testId, $name, $description) {
		//global $sql;

		$query = "UPDATE kontrolltoo k SET k.kt_nimi = '".mysql_escape_string($name)."', k.kirjeldus='".mysql_escape_string($description)."'
		 WHERE k.idkontrolltoo = '".$testId."'";
		$sql->query( $query );
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM kontrolltoo k 
			WHERE k.kt_nimi = '".mysql_escape_string($name)."' AND k.kirjeldus='".mysql_escape_string($description)."'
			 AND k.idkontrolltoo = '".$testId."'";
			$sql->query($query);
			if(count($sql->selectQuery($query))==1){
					return true;
					} else return false;
		} else return true;
	}
	
	
	
	public static function listQuestions($sql, $id) {
		//global $sql;
		$query = "SELECT u.idulesanne, u.tekst, u.vastus, u.vastus2, u.vastus3, u.argumendid, u.maks_punkte  
			FROM ulesanne u
			WHERE u.kontrolltoo ='".$id."'";
		
		$result = $sql->selectQuery( $query );
		if( empty($result)){
			return false;
		} else return $result;
	
	
	}
	
	public static function getTestData($sql, $id) {
		//global $sql;
		$query = "SELECT k.idkontrolltoo, k.kt_nimi, k.kirjeldus  
			FROM kontrolltoo k
			WHERE k.idkontrolltoo ='".$id."'";
		
		$result = $sql->selectQuery( $query );
		if( empty($result)){
			return false;
		} else return $result[0];
	
	
	}
	
	public static function getMaxPoints($sql, $id) {
		//global $sql;
		$query = "SELECT SUM( u.maks_punkte ) AS maksimum
			FROM ulesanne u
			WHERE u.kontrolltoo ='".$id."'";
		
		$result = $sql->selectQuery( $query );
		if( empty($result)){
			return false;
		} else return $result[0]['maksimum'];
	
	
	}
	
	/**
	 
	 * checks if the user already has a finished reult for this test
	 * 
	 * @param $sql $sql
	 * @param int $id
	 */
	public static function checkIfFinished($sql, $testId) {
		//global $sql;
		$activeUser = $_SESSION['userId'];
		$query = "SELECT * 
			FROM sooritus s WHERE s.kontrolltoo = '".$testId."' AND
			s.kasutaja='".$_SESSION["userId"]."' AND s.lopetatud = '1'";
		
		$result = $sql->selectQuery( $query );
		if( empty($result)){
			return false;
		} else return true;
	
	
	}
	
	
}
?>
